package com.android.common.mvp.view.fragment;

import android.view.View;
import android.widget.ImageView;
import com.android.common.R;
import com.android.common.base.BaseFragment;
import com.android.common.constant.Constant;
import com.android.common.constant.IntentKey;
import com.android.common.utils.LogUtil;
import com.android.common.utils.SpUtils;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.player.PlayerFactory;
import com.shuyu.gsyvideoplayer.utils.OrientationUtils;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import java.util.HashMap;
import java.util.Map;
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;

/**
 * 视频播放Fragment
 */
public class VideoPlayFragment extends BaseFragment {

    private static final String TAG = "VideoPlay";

    private StandardGSYVideoPlayer videoPlayer;
    private OrientationUtils orientationUtils;
    private String mUrl, mTitle;

    @Override
    protected int onSetRootViewId() {
        return R.layout.fragment_video_play;
    }

    @Override
    protected boolean onNeedToolBar() {
        return false;
    }

    protected void onFindView() {
        init();
        videoPlayer = mRootView.findViewById(R.id.video_player);
    }

    /* 初始化操作 */
    private void init() {
        //EXOPlayer内核，支持格式更多
        PlayerFactory.setPlayManager(Exo2PlayerManager.class);
        //代理缓存模式，支持所有模式，不支持m3u8等，默认
        //CacheFactory.setCacheManager(ProxyCacheManager.class);
        //系统内核模式
        //PlayerFactory.setPlayManager(SystemPlayerManager.class);
        //ijk内核，默认模式
        //PlayerFactory.setPlayManager(IjkPlayerManager.class);
    }

    @Override
    protected void onInitView() {
        super.onInitView();
        mUrl = mBundle.getString(IntentKey.URL_KEY, "");
        mTitle = mBundle.getString(IntentKey.TITLE_KEY, "");
        LogUtil.d(TAG, "play url = " + mUrl);
        LogUtil.d(TAG, "play title = " + mTitle);
//        String source1 = "https://res.exexm.com/cw_145225549855002";
//        String source1 = "http://ioc.bonc.com.cn:8081/bzfyj/bzf-business-file/fileManage/downloadFileRecord?fileId=9720";
        /**
         * 块文件上传播放测试文件
         */
        //String source1 = "http://172.16.17.119:8114/api/bzf-business-file/fileManage/downloadFileRecord?fileId=11255";

        HashMap<String, String> header = new HashMap<>();
        Map<String, String> mapHeadData = videoPlayer.getMapHeadData();
        if (mapHeadData != null) header.putAll(mapHeadData);
        String token = SpUtils.getString(Constant.TOKEN_KEY);
        header.put(Constant.TOKEN_KEY, token);
        videoPlayer.setUp(mUrl, false, null, header, mTitle);


        //增加封面
        ImageView imageView = new ImageView(mActivity);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setImageResource(R.mipmap.default_ad);
        videoPlayer.setThumbImageView(imageView);
        //增加title
        videoPlayer.getTitleTextView().setVisibility(View.VISIBLE);
        //设置返回键
        videoPlayer.getBackButton().setVisibility(View.VISIBLE);
        //设置旋转
        orientationUtils = new OrientationUtils(mActivity, videoPlayer);
        //设置全屏按键功能,这是使用的是选择屏幕，而不是全屏
        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ------- ！！！如果不需要旋转屏幕，可以不调用！！！-------
                // 不需要屏幕旋转，还需要设置 setNeedOrientationUtils(false)
                //orientationUtils.resolveByClick();
                mActivity.finish();
            }
        });
        //是否可以滑动调整
        videoPlayer.setIsTouchWiget(true);
        //设置返回按键功能
        videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //释放所有
                videoPlayer.setVideoAllCallBack(null);
                mActivity.finish();
            }
        });

        ///不需要屏幕旋转
        videoPlayer.setNeedOrientationUtils(false);
        videoPlayer.startPlayLogic();
    }

    @Override
    public void onPause() {
        super.onPause();
        videoPlayer.onVideoPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        videoPlayer.onVideoResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        GSYVideoManager.releaseAllVideos();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }


}